package com.xh3140.android.widget.dialog.view;

import android.content.Context;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;

import com.xh3140.android.widget.dialog.params.TitleParams;
import com.xh3140.android.widget.dialog.utils.LayoutParamsBuilder;
import com.xh3140.android.widget.dialog.utils.ViewUtils;

/**
 * 对话框首部视图布局中标题视图
 */
public class TitleView extends AppCompatTextView {

    public TitleView(@NonNull Context context) {
        this(context, new TitleParams(context));
    }

    public TitleView(@NonNull Context context, @NonNull TitleParams params) {
        super(context);
        configView(params);
    }

    /**
     * 配置视图
     *
     * @param params 标题视图配置属性
     */
    public void configView(@NonNull TitleParams params) {
        setText(params.text);
        setTextSize(params.textSize);
        setTextColor(params.textColor);
        setTypeface(getTypeface(), params.typeface);
        setGravity(params.gravity);
        setBackgroundColor(params.backgroundColor);
        ViewUtils.setPaddingRelative(this, params.padding);
        setLayoutParams(LayoutParamsBuilder.ofMW(LinearLayoutCompat.LayoutParams.class)
                .setGravity(Gravity.CENTER_HORIZONTAL)
                .setMarginsRelative(params.margins)
                .build());
        ViewUtils.setTextViewHeight(this, params.height);
        setVisibility(params.text == null ? GONE : VISIBLE);
        if (params.callback != null) {
            params.callback.callback(this);
        }
    }
}
